"use strict";
var ToolBar = function(camProp) {
    this.prop = camProp;
};

ToolBar.prototype = {
    fullscreen: function() {
        var element = document.documentElement;
        if (element.requestFullscreen) {
            element.requestFullscreen();
        } else if (element.msRequestFullscreen) {
            element.msRequestFullscreen();
        } else if (element.mozRequestFullScreen) {
            element.mozRequestFullScreen();
        } else if (element.webkitRequestFullscreen) {
            element.webkitRequestFullscreen();
        }
    },
    exitFullscreen: function() {
        if (document.exitFullscreen) {
          document.exitFullscreen();
        } else if (document.msExitFullscreen) {
            document.msExitFullscreen();
        } else if (document.mozCancelFullScreen) {
            document.mozCancelFullScreen();
        } else if (document.webkitExitFullscreen) {
            document.webkitExitFullscreen();
        }

    },

    toggleView: function() {
        if($("#angleView")[0].src.indexOf('select') >= 0) {
          this.tweenView(0);
          $("#angleView").attr('src', 'image/asteroid_normal.png');
        }
        else {
          this.tweenView(1);
          $("#angleView").attr('src', 'image/asteroid_selected.png');
        }
    },

    toggleVR: function(reticle) {
        this.prop.isStereoEnable = !this.prop.isStereoEnable;
        reticle.setLoadingEffect(this.prop.isStereoEnable,this.prop.renderer);
        if(this.prop.isStereoEnable) {
          $("#stereo").attr('src', 'image/vr_selected.png');
        }
        else {
          $("#stereo").attr('src', 'image/vr_normal.png');
        }
    },

       /* 控制陀螺仪 */
    toggleGyro: function(renderer) {
         console.log("toggle gyro ..");
         if (this.prop.orientationControls) {
            this.prop.orientationControls.disconnect();
            this.prop.orientationControls = null;
            $("#gyro").attr('src', 'image/gyroscope_normal.png');
         } else {
            this.prop.sceneManager.toggleDeviceOrientation();
          }
    },

    toggleViewMode: function() {
        this.prop.editMode = !this.prop.editMode;
        if(this.prop.editMode) {
          $("#viewMode").attr('src', 'image/view_disable.png');
        }
        else {
          $("#viewMode").attr('src', 'image/view_normal.png');
        }
    },

    toggleFullscreen: function() {
        if (this.prop.isFullscreenEnabel === 0) {
            this.prop.isFullscreenEnabel = 1;
            $("#full").attr('src', 'image/fullscreen.png');
            this.exitFullscreen();
          } else {
            this.prop.isFullscreenEnabel = 0;
            $("#full").attr('src', 'image/fullscreen_exit.png');
            this.fullscreen();
          }
    },

    tweenView: function(type) {
        console.log(type);
        var self = this;
	    	switch(type) {
	    		case 0:
					var tween = new TWEEN.Tween(self.prop.camera.position)
										 .to({ x: 90, y : 0, z : 100 }, 2500)
										 .easing(TWEEN.Easing.Quadratic.InOut)
										 .onUpdate(function() {
                       
                        if(self.prop.camera.fov > self.prop.fov) {
  										        self.prop.camera.fov -= 3;
  										        if(self.prop.camera.fov < self.prop.fov) self.prop.camera.fov = self.prop.fov;
  										        self.prop.camera.updateProjectionMatrix();
  										 	}
										 })
          					 .start();
          // new TWEEN.Tween( { lat : 0, y : self.prop.camera.position.y, fov : self.prop.camera.fov } )
          //       .to( { lat: 90, y : 0, fov : 100 }, 2500 )
          //       .easing(TWEEN.Easing.Cubic.InOut)
          //       .onUpdate(function() {
          //           let phi = THREE.Math.degToRad( this.lat );
          //           self.prop.camera.target.y = -500 * Math.cos( phi );
          //           self.prop.camera.target.z = -500 * Math.sin( phi );

          //           self.prop.camera.position.y = this.y;
          //           self.prop.camera.fov = this.fov;
          //           self.prop.camera.updateProjectionMatrix();
          //       })
          //       .start()  
	    			break;
	    		case 1:
					var tween = new TWEEN.Tween(self.prop.camera.position)
										 .to({x:0,y:199,z:0}, 1500)
										 .easing(TWEEN.Easing.Quadratic.InOut)
										 .onUpdate(function() {
                      
										 	if(self.prop.camera.fov < self.prop.fovMax) {
                        self.prop.camera.fov += 3;
										        if(self.prop.camera.fov > self.prop.fovMax) self.prop.camera.fov = self.prop.fovMax;
										        self.prop.camera.updateProjectionMatrix();
										 	}
										 })
          					 .start();
          
	    			break;
	    	}
	},
};